package sword_to_1;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_57 {
    public static void main(String[] args) {

    }
    private static int[] twoSum2(int[] nums, int target) {
        int n = nums.length;
        int l = 0, r = n - 1;
        while (l < r){
            int sum = nums[l] + nums[r];
            if (sum == target){
                return new int[]{nums[l], nums[r]};
            }else if (sum < target){
                l++;
            }else {
                r--;
            }
        }
        return null;
    }
    private static int[] twoSum(int[] nums, int target) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (num >= target) {
                break;
            }
            set.add(num);
        }
        for (int num : nums) {
            if (num >= target){
                return nums;
            }
            if (set.contains(target - num)){
                return new int[]{num, target - num};
            }
        }
        return null;
    }
}
